package com.leo.stopwatch;
/*Copyright (c) 2010-2012, Daniel Bostwick

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. Neither the name of Daniel Bostwick nor the names
   of its contributors may be used to endorse or promote products derived
   from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.*/
import java.util.ArrayList;
import java.util.List;

public class Stopwatch {
	
	/**
	 * Implements a method that returns the current time, in milliseconds.
	 * Used for testing
	 */
	public interface GetTime {
		public long now();
	}
	
	/**
	 * Default way to get time. Just use the system clock.
	 */
	private GetTime SystemTime = new GetTime() {
		@Override
		public long now() {	return System.currentTimeMillis(); }
	};
	
	/**
	 * What is the stopwatch doing?
	 */
	public enum State { PAUSED, RUNNING };
	
	private GetTime m_time;
	private long m_startTime;
	private long m_stopTime;
	private long m_pauseOffset;
	private List<Long> m_laps = new ArrayList<Long>();
	private State m_state;
	
	public Stopwatch() {
		m_time = SystemTime;
		reset();
	}
	public Stopwatch(GetTime time) {
		m_time = time;
		reset();
	}
	
	/**
	 * Start the stopwatch running. If the stopwatch is already running, this
	 * does nothing. 
	 */
	public void start() {
		if ( m_state == State.PAUSED ) {
			m_pauseOffset = getElapsedTime();
			m_stopTime = 0;
			m_startTime = m_time.now();
			m_state = State.RUNNING;
		}
	}

	/***
	 * Pause the stopwatch. If the stopwatch is already running, do nothing.
	 */
	public void pause() {
		if ( m_state == State.RUNNING ) {
			m_stopTime = m_time.now();
			m_state = State.PAUSED;
		}
	}

	/**
	 * Reset the stopwatch to the initial state, clearing all stored times. 
	 */
	public void reset() {
		m_state = State.PAUSED;
		m_startTime 	= 0;
		m_stopTime 		= 0;
		m_pauseOffset 	= 0;
		m_laps.clear();
	}
	
	/**
	 * Record a lap at the current time.
	 */
	public void lap() {
		m_laps.add(+getElapsedTime());
	}
	
	/***
	 * @return The amount of time recorded by the stopwatch, in milliseconds
	 */
	public long getElapsedTime() {
		if ( m_state == State.PAUSED ) {
			return (m_stopTime - m_startTime) + m_pauseOffset;
		} else {
			return (m_time.now() - m_startTime) + m_pauseOffset;
		}
	}
	
	/**
	 * @return A list of the laps recorded. Each lap is given as a millisecond
	 * 		   value from when the stopwatch began running. 
	 */
	public List<Long> getLaps() {
		return m_laps;
	}
	
	/**
	 * @return true if the stopwatch is currently running and recording
	 * 		   time, false otherwise.
	 */
	public boolean isRunning() {
		return (m_state == State.RUNNING);
	}
}
